package nicewarm.com.airbansearchanimation.fragment;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import nicewarm.com.airbansearchanimation.R;
import nicewarm.com.airbansearchanimation.Utils;
import nicewarm.com.airbansearchanimation.adapter.HuaweiFirstpageRecylerAdapter;
import nicewarm.com.airbansearchanimation.model.AppListItem;
import nicewarm.com.airbansearchanimation.model.HuaweiFirstPageMultipleItem;
import nicewarm.com.airbansearchanimation.view.ViewUpSearch;


public class FirstPageFragment extends Fragment {

    private static final String TAG = FirstPageFragment.class.getSimpleName();
    private RecyclerView mRecyclerView;
    private HuaweiFirstpageRecylerAdapter mAdapter;
    private ViewUpSearch search;
    private View replaceStatusBar;
    private LinearLayoutManager mLayoutManager;
    private List<HuaweiFirstPageMultipleItem> mItems = new ArrayList<>();
    private int totalDy;
    private boolean isExpand, needtranslation;
    private AlphaTabBarListener mMoveWithSearchAnimationListener;

    public interface AlphaTabBarListener {
        void alphaTabBar(int totalDy, int itemheight);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMoveWithSearchAnimationListener = (AlphaTabBarListener) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_first_page, container, false);
        replaceStatusBar = mRootView.findViewById(R.id.replace_statusbar);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.my_recycler_view);
        search = (ViewUpSearch) mRootView.findViewById(R.id.search);
        mRecyclerView.setHasFixedSize(true);
        isExpand = true;
        needtranslation = true;
        resetReplaceStatusBarHeight();


        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int position = mLayoutManager.findFirstVisibleItemPosition();


                if (position == 0) {
                    View firstVisiableChildView = mLayoutManager.findViewByPosition(position);
                    int itemHeight = firstVisiableChildView.getHeight();
                    totalDy = (position) * itemHeight - firstVisiableChildView.getTop();
                    mMoveWithSearchAnimationListener.alphaTabBar(totalDy, itemHeight/2);
                    alphaStatBar(totalDy, itemHeight/2);

                    int searchPaddingTop = getResources().getDimensionPixelOffset(R.dimen.search_margintop);
                    int newSearchPaddingTop = searchPaddingTop - Utils.getStatusBarHeight(getActivity()) - getResources().getDimensionPixelSize(R.dimen.tab_bar_height);
                    if (totalDy >= newSearchPaddingTop) {
                        if (needtranslation) {
                            search.setTranslationY(-newSearchPaddingTop);
                        }
                        if (isExpand) {
                            search.updateShow(false);
                            isExpand = false;
                            needtranslation = false;
                        }
                    } else {
                        if (needtranslation) {
                            search.setTranslationY(-totalDy);
                        }
                        if (!isExpand) {
                            search.updateShow(true);
                            isExpand = true;
                            needtranslation = true;
                        }
                    }
                }
            }
        });

        return mRootView;
    }

    private void alphaStatBar(int totalDy, int itemheight) {
        double progress = (double) totalDy / itemheight;
        double alpha = 255 * progress;
        if (alpha > 255) {
            alpha = 255;
        }
        replaceStatusBar.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
    }

    private void resetReplaceStatusBarHeight() {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) replaceStatusBar.getLayoutParams();
        params.height = Utils.getStatusBarHeight(getActivity());
        replaceStatusBar.setLayoutParams(params);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        mAdapter = new HuaweiFirstpageRecylerAdapter(getActivity(), mItems);
        mRecyclerView.setAdapter(mAdapter);
    }

    private HuaweiFirstPageMultipleItem getBannerItemMock() {
        HuaweiFirstPageMultipleItem item = new HuaweiFirstPageMultipleItem();
        item.setItemType(HuaweiFirstPageMultipleItem.ITEM_LAYOUT_BANNER);
        List<String> bannerImages = new ArrayList<>();
        bannerImages.add("http://img5.imgtn.bdimg.com/it/u=1286418634,1650511831&fm=23&gp=0.jpg");
        bannerImages.add("http://img4.imgtn.bdimg.com/it/u=1361018452,2563399722&fm=23&gp=0.jpg");
        bannerImages.add("http://img1.imgtn.bdimg.com/it/u=3343072355,2921806608&fm=23&gp=0.jpg");
        bannerImages.add("http://img5.imgtn.bdimg.com/it/u=4153865610,3964593051&fm=23&gp=0.jpg");
        bannerImages.add("http://img3.imgtn.bdimg.com/it/u=831382975,126019568&fm=23&gp=0.jpg");
        bannerImages.add("http://img0.imgtn.bdimg.com/it/u=2151764897,4166440523&fm=23&gp=0.jpg");
        item.setImages(bannerImages);
        return item;
    }

    private AppListItem getAppItemMock(String appName, String appSize, String appIconUrl) {
        AppListItem appitem = new AppListItem();
        appitem.setAppName(appName);
        appitem.setAppSize(appSize);
        appitem.setImageUrl(appIconUrl);
        return appitem;
    }

    private HuaweiFirstPageMultipleItem getAppListItem(String category) {
        HuaweiFirstPageMultipleItem item = new HuaweiFirstPageMultipleItem();
        item.setItemType(HuaweiFirstPageMultipleItem.ITEM_LAYOUT_GALLERY);
        item.setAppCategory(category);
        List<AppListItem> appList = new ArrayList<>();
        appList.add(getAppItemMock("微信", "50M", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1444233970,1879862484&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("微聊", "40M", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=563352902,2435439847&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("互动吧", "32.5M", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2939076417,814963603&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("个性资讯", "50M", "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1559014756,4039497930&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("多家", "50M", "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1540806586,1277677400&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("去哪儿", "50M", "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2598935778,3454960925&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("胡同", "50M", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2244640860,2393150960&fm=23&gp=0.jpg"));
        appList.add(getAppItemMock("手机卫士", "50M", "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1950395894,1330886173&fm=23&gp=0.jpg"));
        item.setApps(appList);
        return item;
    }

    private HuaweiFirstPageMultipleItem getTwoImageItemMock() {
        HuaweiFirstPageMultipleItem item = new HuaweiFirstPageMultipleItem();
        item.setItemType(HuaweiFirstPageMultipleItem.ITEM_LAYOUT_TWO_IMAGE);
        List<String> images2 = new ArrayList<>();
        images2.add("http://img0.imgtn.bdimg.com/it/u=907904932,1584472262&fm=21&gp=0.jpg");
        images2.add("http://car0.autoimg.cn/car/upload/2015/3/28/20150328103350884264112.jpg");
        item.setImages(images2);
        return item;
    }

    private void initData() {

        mItems.add(getBannerItemMock());
        mItems.add(getAppListItem("精品推荐"));
        mItems.add(getAppListItem("精品新游"));
        mItems.add(getTwoImageItemMock());
        mItems.add(getAppListItem("大家都在用"));
        mItems.add(getAppListItem("这些最好玩"));
        mItems.add(getTwoImageItemMock());
        mItems.add(getAppListItem("新游预约"));
        mItems.add(getAppListItem("精品尝鲜，每周五上新"));
//        mItems.add(getTwoImageItemMock());
//        mItems.add(getAppListItem("付费精品"));
//        mItems.add(getAppListItem("大家都在玩的游戏"));
//        mItems.add(getTwoImageItemMock());
//        mItems.add(getAppListItem("你可能会喜欢"));
//        mItems.add(getAppListItem("同机型用户喜爱"));
//        mItems.add(getTwoImageItemMock());
//        mItems.add(getAppListItem("拍摄美化，发现更多美好"));
//        mItems.add(getAppListItem("休闲益智，想玩就玩"));

    }
}
